//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        //TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
        // to see how IntelliJ IDEA suggests fixing it.
        int[] nums = {7, 3, 2, 6};
        System.out.println("Original array:");
        printArray(nums);

        mergeSort(nums, 0, nums.length - 1);

        System.out.println("\nSorted array:");
        printArray(nums);
    }

    static void mergeSort(int[] nums, int left, int right) {
        if (left >= right)
            return;

        int mid = (left + right) / 2;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);

        merge(nums, left, mid, right);
    }

    static void merge(int[] nums, int left, int mid, int right) {
        int[] tmp = new int[right - left + 1];
        int i = left, j = mid + 1, k = 0;

        while (i <= mid && j <= right) {
            if (nums[i] <= nums[j])
                tmp[k++] = nums[i++];
            else
                tmp[k++] = nums[j++];
        }

        while (i <= mid)
            tmp[k++] = nums[i++];

        while (j <= right)
            tmp[k++] = nums[j++];

        for (k = 0; k < tmp.length; k++) {
            nums[left + k] = tmp[k];
        }
    }

    static void printArray(int[] nums) {
        for (int num : nums) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}
